package com.ruoyi.make.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.make.domain.*;
import com.ruoyi.system.api.CodeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.make.mapper.ScgdProductionWorkOrderMapper;
import com.ruoyi.make.service.IScgdProductionWorkOrderService;

import javax.annotation.Resource;

/**
 * 生产工单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-26
 */
@Service
public class ScgdProductionWorkOrderServiceImpl implements IScgdProductionWorkOrderService {
    @Resource
    private ScgdProductionWorkOrderMapper scgdProductionWorkOrderMapper;

    @Resource
    private CodeClient codeClient;

    /**
     * 查询生产工单
     *
     * @param gdCode 生产工单主键
     * @return 生产工单
     */
    @Override
    public ScgdProductionWorkOrder selectScgdProductionWorkOrderByGdCode(String gdCode) {
        return scgdProductionWorkOrderMapper.selectScgdProductionWorkOrderByGdCode(gdCode);
    }

    /**
     * 查询生产工单列表
     *
     * @param scgdProductionWorkOrder 生产工单
     * @return 生产工单
     */
    @Override
    public List<ScgdProductionWorkOrder> findAll(ScgdProductionWorkOrder scgdProductionWorkOrder) {
        return scgdProductionWorkOrderMapper.findAll(scgdProductionWorkOrder);
    }

    /**
     * 新增生产工单
     *
     * @param scgdProductionWorkOrder 生产工单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScgdProductionWorkOrder(ScgdProductionWorkOrder scgdProductionWorkOrder) {
        scgdProductionWorkOrder.setCreateTime(DateUtils.getNowDate());
        scgdProductionWorkOrder.setGdCode(codeClient.getCode(18));
        scgdProductionWorkOrder.setCpCppcCode(codeClient.getCode(19));
        int rows = scgdProductionWorkOrderMapper.insertScgdProductionWorkOrder(scgdProductionWorkOrder);
//        insertScddProductionOrderForm(scgdProductionWorkOrder);
        return rows;
    }

    /**
     * 查询生产工单关联Bom
     *
     * @param gdCode 生产工单主键
     * @return 生产工单
     */
    @Override
    public List<WldaMaterialFile> selectGdAndBom(String gdCode){
        return scgdProductionWorkOrderMapper.selectGdAndBom(gdCode);
    }


    /**
     * 修改工单状态
     *
     * @param gdCode 生产工单主键
     * @return 生产工单
     */
    @Transactional
    @Override
    public int updateGdStatus(String gdCode,String status) {
        return scgdProductionWorkOrderMapper.updateGdStatus(gdCode,status);
    }


    /**
     * 查询工序下的班组
     *
     * @param gxCode 产品编码
     * @return 结果
     */
    @Override
    public List<BzTeamSchedule> selectBzByGxCode(String gxCode) {
        return scgdProductionWorkOrderMapper.selectBzByGxCode(gxCode);
    }


    /**
     * 查询工序
     *
     * @param cpCode 产品编码
     * @return 结果
     */
    @Override
    public List<GxProcess> selectGxByCpCode(String cpCode) {
        return scgdProductionWorkOrderMapper.selectGxByCpCode(cpCode);
    }


    /**
     * 修改生产工单
     *
     * @param scgdProductionWorkOrder 生产工单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScgdProductionWorkOrder(ScgdProductionWorkOrder scgdProductionWorkOrder) {
        scgdProductionWorkOrder.setUpdateTime(DateUtils.getNowDate());
        scgdProductionWorkOrderMapper.deleteScddProductionOrderFormByDdCode(scgdProductionWorkOrder.getGdCode());
        return scgdProductionWorkOrderMapper.updateScgdProductionWorkOrder(scgdProductionWorkOrder);
    }

    /**
     * 批量删除生产工单
     *
     * @param gdCodes 需要删除的生产工单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScgdProductionWorkOrderByGdCodes(String[] gdCodes) {
        scgdProductionWorkOrderMapper.deleteScddProductionOrderFormByDdCodes(gdCodes);
        return scgdProductionWorkOrderMapper.deleteScgdProductionWorkOrderByGdCodes(gdCodes);
    }

    /**
     * 删除生产工单信息
     *
     * @param gdCode 生产工单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScgdProductionWorkOrderByGdCode(String gdCode) {
        scgdProductionWorkOrderMapper.deleteScddProductionOrderFormByDdCode(gdCode);
        return scgdProductionWorkOrderMapper.deleteScgdProductionWorkOrderByGdCode(gdCode);
    }

    /**
     * 新增生产订单信息
     *
     * @param scgdProductionWorkOrder 生产工单对象
     */
    public void insertScddProductionOrderForm(ScgdProductionWorkOrder scgdProductionWorkOrder)
    {
        ScddProductionOrderForm scddProductionOrderForm = scgdProductionWorkOrder.getScddProductionOrderForm();
        String gdCode = scgdProductionWorkOrder.getGdCode();
        if (StringUtils.isNotNull(scddProductionOrderForm)) {

            scddProductionOrderForm.setDdCode(gdCode);
        }
        scgdProductionWorkOrderMapper.insertScgdProductionWorkOrder(scgdProductionWorkOrder);
    }
}
